import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'tansactionRecordquiryM',
  state: {
    columnsInfo: {
      // 详情和发行人基本信息
      1: { columns: [], checkedKeys: [], treeData: [] },
      2: { columns: [], checkedKeys: [], treeData: [] },
      3: { columns: [], checkedKeys: [], treeData: [] },
      4: { columns: [], checkedKeys: [], treeData: [] },
      5: { columns: [], checkedKeys: [], treeData: [] },
      6: { columns: [], checkedKeys: [], treeData: [] },
      7: { columns: [], checkedKeys: [], treeData: [] },
      8: { columns: [], checkedKeys: [], treeData: [] },
      9: { columns: [], checkedKeys: [], treeData: [] },
      10: { columns: [], checkedKeys: [], treeData: [] },
      11: { columns: [], checkedKeys: [], treeData: [] },
      12: { columns: [], checkedKeys: [], treeData: [] },
      13: { columns: [], checkedKeys: [], treeData: [] },
      14: { columns: [], checkedKeys: [], treeData: [] },
      15: { columns: [], checkedKeys: [], treeData: [] },
      16: { columns: [], checkedKeys: [], treeData: [] },
    },
    checkedKeysInfo: {
      // 详情和发行人基本信息
      1: { checkedKeys: [] },
      2: { checkedKeys: [] },
      3: { checkedKeys: [] },
      4: { checkedKeys: [] },
      5: { checkedKeys: [] },
      6: { checkedKeys: [] },
      7: { checkedKeys: [] },
      8: { checkedKeys: [] },
      9: { checkedKeys: [] },
      10: { checkedKeys: [] },
      11: { checkedKeys: [] },
      12: { checkedKeys: [] },
      13: { checkedKeys: [] },
      14: { checkedKeys: [] },
      15: { checkedKeys: [] },
      16: { checkedKeys: [] },
    },
    jyfx: [],
    jysc: [],
  },
  effects: {
    *getZqJyfx({ payload }, { call, put }) {
      const data = yield call(api.tansactionRecordquiry.getZqJyfx, payload);
      yield put({
        type: 'save',
        payload: {
          jyfx: data,
        },
      });
    },

    *getJysc({ payload }, { call, put }) {
      const data = yield call(api.tansactionRecordquiry.getJysc, payload);
      if (data) {
        yield put({
          type: 'save',
          payload: {
            jysc: data,
          },
        });
      }
    },
    //获取token值
    *saveToken({ payload }, { call, put }) {
      const data = yield call(api.saveToken, payload);
      if (data && data.success) {
        return data;
      } else {
        message.error('导出失败');
      }
    },
    //查询获取表头信息
    *queryShowCol({ payload, callback }, { call, put, select }) {
      const res = yield call(api.tansactionRecordquiry.queryShowCol, payload);
      if (res && res.code == 0) {
        const columns = res.data.dataSource.columns;
        const checkedKeys = [];
        const treeData = [
          {
            title: '全部',
            key: 'all',
            children: res.data.dataSource.columns,
          },
        ];

        res.data.dataSource.columns.map((item) => {
          if (item.display == true) {
            checkedKeys.push(item.key);
          }
        });

        if (payload.type == 'BOND_FLOW') {
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            1: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            1: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'FUND_FLOW') {
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            2: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            2: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'ETF_FLOW') {
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            3: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            3: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'DEPOSIT_FLOW') {
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            4: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            4: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'FINANCIAL_MANAGEMENT_PLAN_FLOW') {
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            5: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            5: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'INTER_BANK_REPURCHASE_FLOW') {
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            6: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            6: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'EXCHANGE_REPURCHASE_FLOW') {
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            7: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            7: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'FINANCIAL_MANAGEMENT_FLOW') {
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            8: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            8: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'NON_STANDARD_FLOW') {
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            9: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            9: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'XYSHG_FLOW') {
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            10: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            10: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
        if (payload.type == 'STK_FLOW' || payload.type == 'STK_HK_FLOW') {
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            11: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            11: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
        if (payload.type == 'bond_lending_flow') {
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            12: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            12: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
        if (payload.type == 'PLAN_INFO_FLOW') {
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            13: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            13: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
        if (payload.type == 'OUT_SOURCE_FLOW') {
          //综合查询中流水查询 委外
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            15: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            15: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
        if (payload.type == 'FUTURES_INFO_FLOW') {
          //综合查询中流水查询 股指期货
          let columnsInfo = yield select((state) => state.tansactionRecordquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.tansactionRecordquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            16: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            16: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
      } else {
        message.error(res.message || '查询获取表头信息失败');
      }
    },

    //保存表头信息
    *saveCol({ payload }, { call, put, select }) {
      const res = yield call(api.tansactionRecordquiry.saveCol, payload);
      return res;
    },

    //债券流水表
    *bondTable({ payload, callback }, { call, put, select }) {
      const res = yield call(api.tansactionRecordquiry.bondTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res.message || '查询流水失败');
      }
    },

    //其它tab流水表
    *queryFnd({ payload, callback }, { call, put, select }) {
      const res = yield call(api.tansactionRecordquiry.queryFnd, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res.message || '查询流水失败');
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
